package com.per.mybootall.webSocket;

import com.google.common.collect.Lists;
import com.per.mybootall.pojo.Logistics;
import com.per.mybootall.pojo.User;
import com.per.mybootall.service.impl.WebSocketAddBeanimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;

@ServerEndpoint("/logisticswebsocket")
@Component
public class LogisticsWebSocket {
   private static WebSocketAddBeanimpl websocketAddBeanimpl;
   @Autowired
   public void setwebsocketAdd(WebSocketAddBeanimpl websocketAddBeanimpl){
    LogisticsWebSocket.websocketAddBeanimpl=websocketAddBeanimpl;
   }
   private static List<Logistics> list= Lists.newArrayList();
    @OnOpen
    public void open(Session session){

        String username= session.getQueryString();
        String  name=username.split("=")[1];
//        System.out.println("进入的名字:"+name);
//        User useri= userMapper.selectByUsernameforUser(name);
//
//        System.out.println("user:"+useri.toString());
//        System.out.println("id:"+useri.getId());
//       List<Logistics> list= logisticsMapper.selectIdByName(useri.getId());
//        System.out.println("您有"+list.size()+"条消息未读");
        list=websocketAddBeanimpl.noReadMessage(name);
        String k="未读消息:"+list.size();
        System.out.println("未读消息:"+k);
        try {
            session.getBasicRemote().sendText(k);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

@OnMessage
    public void noreallyEmail(Session session,String msg){
    if("site".equals(msg)){
        System.out.println("site");
        System.out.println(list.toString());
        try {
        for(Logistics logistics:list){
//0:未发送    1：已发送   2：已到达
                   if(logistics.getLogisticsmessage().equals(0)){
                String noreallyEmail="<span style=\"color: #CF1900;\">你的物流即将发送,订单号为："+logistics.getOrderid()+"，请耐心等待</span>";
                System.out.println(noreallyEmail);
                session.getBasicRemote().sendText(noreallyEmail);

                  }else if(logistics.getLogisticsmessage().equals(1)){
                            String noreallyEmail="<span style=\"color: #CF1900;\">你的物流已经发送,订单号为："+logistics.getOrderid()+"</span>";
                            System.out.println(noreallyEmail);
                            session.getBasicRemote().sendText(noreallyEmail);

                    }else if(logistics.getLogisticsmessage().equals(2)){
                        String noreallyEmail="<span style=\"color: #CF1900;\">你的物流已经到达,订单号为："+logistics.getOrderid()+"</span>";
                        System.out.println(noreallyEmail);
                        session.getBasicRemote().sendText(noreallyEmail);
                    }
        }
        } catch (IOException e) {
            e.printStackTrace();
    }

    }
}
    @OnClose
    public void close(Session session){
        System.out.println("管道关闭");
    }
}
